public class E24 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);
        head.next.next.next.next.next = new ListNode(6);

        ListNode res = ReverseList(head);

        while(res != null){
            System.out.print(res.value + "->");
            res = res.next;
        }
        System.out.println("null");
    }

    public static ListNode ReverseList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode pPre = null;
        ListNode pReservedNode = null;
        ListNode pNode = head;
        while(pNode != null){
            ListNode pNext = pNode.next;
            if(pNext == null){
                pReservedNode = pNode;
            }

            pNode.next = pPre;
            pPre = pNode;
            pNode = pNext;
        }
        return pReservedNode;
    }
}
